#include "cCollision.h"
#include "Command.h"

cCollision::cCollision(void)
{
}

cCollision::~cCollision(void)
{
}


void cCollision::Collision()
{
	mLcollisionListIt = mLcollisionList.begin();
	for (LISTFORNUM(mLcollisionList, i))
	{
		mRcollisionListIt = mRcollisionList.begin();
		for (LISTFORNUM(mRcollisionList, j))
		{
			if (Collision(*mLcollisionListIt->PosX, mLcollisionListIt->sizeX, *mRcollisionListIt->PosX, mRcollisionListIt->sizeX))
			{
				if (Collision(*mLcollisionListIt->PosY, mLcollisionListIt->sizeY, *mRcollisionListIt->PosY, mRcollisionListIt->sizeY))
				{
					*mLcollisionListIt->state = eCOLLISION;
					*mRcollisionListIt->state = eCOLLISION;
				}
			}

			if(*mRcollisionListIt->state == eCOLLISION) mRcollisionListIt = mRcollisionList.erase(mRcollisionListIt);
			else ++mRcollisionListIt;
		}
		if(*mLcollisionListIt->state == eCOLLISION) mLcollisionListIt = mLcollisionList.erase(mLcollisionListIt);
		else ++mLcollisionListIt;
	}
}

bool cCollision::Collision(int pL, int pLsize, int pR, int pRsize)
{
	int tVal;

	if (pL <= pR)
	{
 		tVal = pR - pL;

		if (tVal < pLsize)
		{
 			return true;
		}
	}
	else
	{
		tVal = pL - pR;
		if (tVal < pRsize)
		{
			return true;
		}
	}
	return false;
}

void cCollision::LADD(  int* state,  int* X,  int* Y, int pSizeX, int pSizeY )
{
	sRECT tRect = {state, X, Y, pSizeX, pSizeY};

	mLcollisionList.push_back(tRect);
}

void cCollision::RADD(  int* state,  int* X,  int* Y, int pSizeX, int pSizeY )
{
	sRECT tRect = {state, X, Y, pSizeX, pSizeY};

	mRcollisionList.push_back(tRect);
}